﻿namespace Nello.Data
{
    using Nello;
    using System;
    using System.Collections;

    public class SqlUtil
    {
        public static string JoinKeys(Hashtable ht)
        {
            return StringUtil.Join(ht.Keys);
        }

        public static string JoinPairs(Hashtable ht)
        {
            string ret = "";
            IDictionaryEnumerator enu = ht.GetEnumerator();
            while (enu.MoveNext())
            {
                object sValue = enu.Value;
                if (((sValue is string) || (sValue is DateTime)) || (sValue is bool))
                {
                    sValue = "'" + sValue.ToString() + "'";
                }
                object CS$0$0002 = ret;
                ret = string.Concat(new object[] { CS$0$0002, enu.Key.ToString(), "=", sValue, "," });
            }
            return ret.TrimEnd(",".ToCharArray());
        }

        public static string JoinValues(Hashtable ht)
        {
            string ret = "";
            IEnumerator enu = ht.Values.GetEnumerator();
            while (enu.MoveNext())
            {
                object sValue = enu.Current;
                if (((sValue is string) || (sValue is DateTime)) || (sValue is bool))
                {
                    sValue = "'" + sValue.ToString() + "'";
                }
                ret = ret + sValue + ",";
            }
            return ret.TrimEnd(",".ToCharArray());
        }

        public static string JoinWheres(Hashtable ht)
        {
            string ret = "";
            IDictionaryEnumerator ide = ht.GetEnumerator();
            while (ide.MoveNext())
            {
                string key = ide.Key.ToString();
                if (ide.Value != null)
                {
                    string CS$0$0002;
                    string val = ide.Value.ToString();
                    if (key.StartsWith("_numeric_"))
                    {
                        CS$0$0002 = ret;
                        ret = CS$0$0002 + " and " + key.TrimStart("_numeric_".ToCharArray()) + "=" + val;
                    }
                    else
                    {
                        if (key.StartsWith("_string_"))
                        {
                            CS$0$0002 = ret;
                            ret = CS$0$0002 + " and " + key.TrimStart("_string_".ToCharArray()) + "='" + val + "'";
                            continue;
                        }
                        if (key.StartsWith("_like_"))
                        {
                            if (val.StartsWith("="))
                            {
                                CS$0$0002 = ret;
                                ret = CS$0$0002 + " and " + key.TrimStart("_like_".ToCharArray()) + "='" + val + "'";
                            }
                            else
                            {
                                CS$0$0002 = ret;
                                ret = CS$0$0002 + " and " + key.TrimStart("_like_".ToCharArray()) + " like '%" + val + "%'";
                            }
                            continue;
                        }
                        if (key.StartsWith("_left_"))
                        {
                            if (val.StartsWith("="))
                            {
                                CS$0$0002 = ret;
                                ret = CS$0$0002 + " and " + key.TrimStart("_left_".ToCharArray()) + "='" + val + "'";
                            }
                            else
                            {
                                CS$0$0002 = ret;
                                ret = CS$0$0002 + " and " + key.TrimStart("_left_".ToCharArray()) + " like '" + val + "%'";
                            }
                            continue;
                        }
                        if (key.StartsWith("_right_"))
                        {
                            if (val.StartsWith("="))
                            {
                                CS$0$0002 = ret;
                                ret = CS$0$0002 + " and " + key.TrimStart("_right_".ToCharArray()) + "='" + val + "'";
                            }
                            else
                            {
                                CS$0$0002 = ret;
                                ret = CS$0$0002 + " and " + key.TrimStart("_right_".ToCharArray()) + " like '%" + val + "'";
                            }
                            continue;
                        }
                    }
                }
            }
            return ret;
        }

        public static string PopItem(Hashtable ht, string key)
        {
            string ret = ht[key].ToString();
            ht.Remove(key);
            return ret;
        }
    }
}

